package com.heima.leetcode.practice;

import java.util.LinkedList;

/**
 * @author 勾新杰
 * @version 1.0
 * @description: leetcode 232. 用栈实现队列
 * @date 2025/3/4 19:48
 */
public class E232 {
    public static class MyQueue {

        private final LinkedList<Integer> stackIn;

        private final LinkedList<Integer> stackOut;

        public MyQueue() {
            this.stackIn = new LinkedList<>();
            this.stackOut = new LinkedList<>();
        }

        public void push(int x) {
            stackIn.push(x);
        }

        public int pop() {
            if (stackOut.isEmpty()) moveElements();
            return stackOut.pop();
        }

        @SuppressWarnings("all")
        public int peek() {
            if (stackOut.isEmpty()) moveElements();
            return stackOut.peek();
        }

        private void moveElements() {
            while (!stackIn.isEmpty())
                stackOut.push(stackIn.pop());
        }

        public boolean empty() {
            return stackIn.isEmpty() && stackOut.isEmpty();
        }
    }
}
